class point = 
object
  val mutable longitude = 0.0
  val mutable latitude = 0.0
  method get_long = longitude
  method get_lat = latitude
  method set_long long = longitude <- long
  method set_lat lat = latitude <- lat
end;;

let point11 = new point
let point12 = new point 
let point13 = new point
let point14 = new point
let point15 = new point
let point16 = new point;;

	point11#set_lat 5.5;
	point11#set_long 5.5;
	point12#set_lat 6.5;
	point12#set_long 5.5;
	point13#set_lat 7.5;
	point13#set_long 5.5;
	point14#set_lat 8.5;
	point14#set_long 5.5;
	point15#set_lat 9.5;
	point15#set_long 5.5;
	point16#set_lat 10.5;
	point16#set_long 5.5

let point21 = new point
let point22 = new point 
let point23 = new point
let point24 = new point
let point25 = new point
let point26 = new point;;

	point21#set_lat 5.5;
	point21#set_long 9.5;
	point22#set_lat 6.5;
	point22#set_long 9.5;
	point23#set_lat 7.5;
	point23#set_long 9.5;
	point24#set_lat 8.5;
	point24#set_long 9.5;
	point25#set_lat 9.5;
	point25#set_long 9.5;
	point26#set_lat 10.5;
	point26#set_long 9.5

let point31 = new point
let point32 = new point 
let point33 = new point
let point34 = new point
let point35 = new point
let point36 = new point;;

	point31#set_lat 7.5;
	point31#set_long 3.5;
	point32#set_lat 7.5;
	point32#set_long 5.5;
	point33#set_lat 7.5;
	point33#set_long 6.5;
	point34#set_lat 7.5;
	point34#set_long 7.5;
	point35#set_lat 7.5;
	point35#set_long 8.5;
	point36#set_lat 7.5;
	point36#set_long 9.5;;

(*Printf.printf "point21: (%f,%f) \n point22: (%f,%f)" 
point21#get_lat point21#get_long point22#get_lat point22#get_long
*)
let v1 = [|point11; point12;point13;point14;point15;point16|]
let v2 = [|point21; point22;point23;point24;point25;point26|]
let h1 = [|point31; point32;point33;point34;point35;point36|]

let allPaths = [|v1;v2;h1|]

let swap a b t =
	let t_b = t.(b) in t.(b) <- t.(a); t.(a) <- t_b;;

let bubble_sort t =
	let length = Array.length t in
	for max_pos = length - 1 downto 0 do
		for pos = 0 to max_pos - 1 do
			if t.(pos).(0)#get_long > t.(pos + 1).(0)#get_long (* or if they are equal... *)
			then swap pos (pos + 1) t
			else
				if t.(pos).(0)#get_long = t.(pos + 1).(0)#get_long
				then 
					if Array.length t.(pos) = 1 && Array.length t.(pos+1) > 1
					then swap pos (pos + 1) t
					(* make sure horizonal line is before vertical also *)
		done
	done in bubble_sort allPaths;;

let intersection = new point
	if Array.length allPaths.(1) > 1
	then
		if allPaths.(1).(0)#get_long >= allPaths.(0).(1)#get_long && 
			allPaths.(1).(0)#get_long <= allPaths.(0).(Array.length allPaths.(1)-1)#get_long &&
			allPaths.(0).(0)#get_lat <= allPaths.(1).(0)#get_lat &&
			allPaths.(0).(0)#get_lat >= allPaths.(1).(Array.length allPaths.(1)-1)#get_lat
			then
			intersection#set_lat allPaths.(0).(0)#get_lat;
			intersection#set_long allPaths.(1).(0)#get_long;
			Printf.printf "intersection at: (%f,%f)" intersection#get_lat intersection#get_long
	else
		if allPaths.(1).(0)#get_long = allPaths.(0).(0)#get_long &&
			allPaths.(1).(0)#get_lat <= allPaths.(0).(0)#get_lat &&
			allPaths.(1).(0)#get_lat >= allPaths.(0).(Array.length allPaths.(1)-1)#get_lat 
			then
			intersection#set_lat allPaths.(1).(0)#get_lat;
			intersection#set_long allPaths.(0).(0)#get_long;
			Printf.printf "intersection at: (%f,%f)" intersection#get_lat intersection#get_long

(*
let sweep = ref allPaths.(0).(0)#get_long and intersection = new point in
	while (sweep <= ref allPaths.((Array.length allPaths)-1).((Array.length allPaths.(0))-1)#get_long) do 
		
		if allPaths.(0).(0)#get_long <= allPaths.(1).(0)#get_long && 
			allPaths.(0).(0)#get_long >= allPaths.(1).(Array.length allPaths.(1))#get_long
			then
				intersection#set_lat allPaths.(0).(0)#get_lat;
				intersection#set_long allPaths.(1).(0)#get_long;
				Printf.printf "intersection at: (%f,%f)" intersection#get_lat intersection#get_long;
		
		sweep:=!sweep+.0.01
	done
*)
